package yatan.common.utility;

import java.util.Date;

import org.apache.log4j.Logger;

public class LogUtility {
    public static <T extends Throwable> T logError(Logger logger, T e) {
        logger.error(e.getMessage(), e);
        return e;
    }

    public static long logMethodEntrance(Logger log, String method, String[] parameterNames, Object[] parameterValues) {
        StringBuilder sb = new StringBuilder();
        if (parameterNames != null) {
            for (int i = 0; i < parameterNames.length; i++) {
                sb.append(parameterNames[i] + " = " + parameterValues[i] + ", ");
            }
            sb.delete(sb.length() - 2, sb.length());
        }
        log.debug("Enter method '" + method + "' with parameters [" + sb.toString() + "]");

        return new Date().getTime();
    }

    public static <T> T logMethodExit(Logger log, String method, boolean isReturn, T returnValue, long startTime) {
        log.debug("Exit method '" + method + "'" + (isReturn ? ", return = " + returnValue : "") + ", call duration: "
                + (new Date().getTime() - startTime) + " ms");
        return returnValue;
    }
}
